import { reqGoodsInfo } from "@/api"
import {redAddOrUpdateShopCart} from "@/api"
import {getUUID} from "@/utils/uuid_token"
const state={
    goodInfo:{},
    // 游客的临时身份
    uuid_token:getUUID()
}
const mutations={
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo
    },
}
const actions={
    // 获取产品信息的action
    async getGoodInfo({commit},skuId){
       let result= await reqGoodsInfo(skuId)
    //    console.log(result);
        if(result.code == 200){
            commit("GETGOODINFO",result.data)
        }
    },

    // 将产品信息添加到购物车中
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result = await redAddOrUpdateShopCart(skuId,skuNum)
        // console.log(result);
        if(result.code == 200){
            return "ok"
        }else{
            return Promise.reject(new Error("faile"))
        }
        
    }
}

const getters={
    // 路径导航简化的数据
    categoryView(state){
        return state.goodInfo.categoryView || {}
    },
    // 简化产品信息的数据
    skuInfo(state){
        return state.goodInfo.skuInfo || {}
    },
    // 产品售卖属性的简化
    spuSaleAttrList(state){
        return state.goodInfo.spuSaleAttrList || []
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}
    
